package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Multimap;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysFields;
import com.ruoyi.system.domain.SysSort;
import com.ruoyi.system.domain.SysTable;
import com.ruoyi.system.service.ISysFieldsService;
import com.ruoyi.system.service.ISysTableService;
import com.ruoyi.web.domain.Solr;
import com.ruoyi.web.service.ISysQueryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检索Controller
 */
@RestController
@RequestMapping("/system/query")
public class QueryController  extends BaseController {
    @Autowired
    private ISysQueryService sysQueryService;

    @Autowired
    private ISysFieldsService sysFieldsService;

    /**
     * 用户选择 高级检索
     * @return
     */
    @ApiOperation("高级检索")
    @ApiImplicitParam(name = "systable", value = "表对象", required = true, dataType = "SysTable", paramType = "传表对象")
    @GetMapping("/list")
    public TableDataInfo list(SysTable systable)
    {
        startPage();
        List<Object> objectList = new ArrayList<>();
        // 拿到这张表的字段
        SysFields sysFields = new SysFields();
        sysFields.setTableNo(systable.getRecNo());
        List<SysFields> sysField = sysFieldsService.selectSysFieldsByTableId(sysFields);
        List<Object> list = sysQueryService.queryByUser(systable);
        objectList.add(sysField);
        objectList.add(list);
        return getDataTable(objectList);
    }

    /**
     * 综合检索
     * @return
     */
    @ApiOperation("综合检索")
    @ApiImplicitParam(name = "sysSort", value = "分类对象", required = true, dataType = "SysSort", paramType = "传分类对象")
    @GetMapping
    public TableDataInfo query(SysSort sysSort)
    {
        startPage();
        List<Object> objectList = new ArrayList<>();
        SysFields sysFields = new SysFields();
        sysFields.setTableNo(sysSort.getSortMainTableNo());
        List<SysFields> sysField = sysFieldsService.selectSysFieldsByTableId(sysFields);
        List<Object> list = sysQueryService.querySynthesize(sysSort);
        objectList.add(sysField);
        objectList.add(list);
        return getDataTable(objectList);
    }

    /**
     * 查询 树形列表
     * @param recNo
     * @return
     */
    @ApiOperation("查询 树形列表")
    @ApiImplicitParam(name = "recNo", value = "主表编号", required = true, dataType = "String", paramType = "传主表编号")
    @GetMapping("tree")
    public AjaxResult selectTree(String recNo){
        Map<String, Object> sysTableList = sysQueryService.selectListTree(recNo);
        return AjaxResult.success(sysTableList);
    }

    /**
     *  树形列表 点击详情
     * @param id
     * @return
     */
    @ApiOperation("树形列表 点击详情")
    @ApiImplicitParam(name = "id,tableName", value = "用户自建表id,表名", required = true, dataType = "String", paramType = "传表id,表名")
    @GetMapping("get")
    public AjaxResult selectTreeDetail(String id, String tableName){
        List<SysFields> list = sysQueryService.selectTreeDetail(id, tableName);
        return AjaxResult.success(list);
    }

    /**
     *  图片 只做显示,没有上传
     * @param sortNo
     * @return
     */
    @ApiOperation("获取图片地址")
    @ApiImplicitParam(name = "sortNo", value = "分类编号", required = true, dataType = "String", paramType = "传分类编号")
    @GetMapping("picture")
    public AjaxResult picture(String sortNo){
        List<Object> picture = sysQueryService.picture(sortNo);
        return AjaxResult.success(picture);
    }
}
